package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.data.RecruitListData;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IRecruitAdd;
import com.wbao.dianniu.listener.IRecruitAddListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

public class RecruitAddManager implements IRecruitAdd {

	private IRecruitAddListener mListener;
	private Context mContext;
	
	public RecruitAddManager(Context context) {
		mContext = context;
	}
	
	@Override
	public void recruitAdd(Integer id, int accountId, String post,
			String salary, String tryout, String experience,
			int recruitCount, String education, String describe,
			String address) {
		StringEntity stringEntity = null;
		try {
			Batch batch = new Batch();
			batch.setToken(GlobalContext.getCacheToken());
			Command command = new Command();
			command.addAction(Action.ACTION_ACCOUNT_RECRUIT_ADD)
					.addArgs("id", id)
					.addArgs("accountId", accountId).
					addArgs("post", post).
					addArgs("salary", salary).
					addArgs("tryout", tryout).
					addArgs("experience", experience).
					addArgs("recruitCount", recruitCount).
					addArgs("education", education).
					addArgs("describe", describe).
					addArgs("address", address);
			batch.addCommand(command);
			stringEntity = new StringEntity(batch.toJson(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		HttpUtil http = new HttpUtil();
		http.req(mContext, stringEntity, new MyResponse() {

			@Override
			public void responseSucess(Object obj) {
				// TODO Auto-generated method stub
				RecruitListData recData = JsonUtil.toObject(obj, RecruitListData.class);
				// 通知界面
				if (null != mListener)
					mListener.onRecruitAddSuccess(recData);
			}

			@Override
			public void responseFailure(int errorCode, String msg) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onRecruitAddFailure(errorCode, msg);
			}
		});
	}

	@Override
	public boolean addRecruitAddListener(IRecruitAddListener listener) {
		mListener = listener;
		return mListener == null ? false : true;
	}

	@Override
	public boolean removeRecruitAddListener(IRecruitAddListener listener) {
		if (null == listener)
			return false;
		mListener = null;
		return true;
	}
	
}
